package com.wjk.easy.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.wjk.easy.R;
import com.wjk.easy.db.Exam;
import com.wjk.easy.db.ExamData;
import com.wjk.easy.utils.CommonUtil;

import org.litepal.LitePal;
import org.litepal.crud.async.UpdateOrDeleteExecutor;
import org.litepal.crud.callback.UpdateOrDeleteCallback;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class ExamDataAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<ExamData> list;

    public ExamDataAdapter(Context context, List<ExamData> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_exam_data, parent,
                false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        // TextView tv_card_id, tv_weight, tv_create_time, tv_size;
        final ExamData exam = list.get(position);
        MyViewHolder h = (MyViewHolder) holder;
        h.tv_card_id.setText(exam.getCardId());
        h.tv_weight.setText(exam.getWeight()+"");
        h.tv_create_time.setText(CommonUtil.dateToStr(exam.getCreateTime()));
        h.tv_size.setText(exam.getSizeWidth()+"*"+exam.getSizeHeight());
        h.tv_data_remark.setText(exam.getRemark());

        h.ll_data_container.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                new AlertDialog.Builder(context)
                        .setTitle("删除实验")
                        .setMessage("确定要删除" + exam.getCardId() + "吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                removeData(position);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).show();

                    return true;
            }
        });


        if(position%2==0){
            h.ll_data_container.setBackgroundColor(context.getResources().getColor(R.color.trans));
        }else{
            h.ll_data_container.setBackgroundColor(context.getResources().getColor(R.color.colorSelectDarkTrans));
        }
    }

    public void setEmptyListener(EmptyListener emptyListener){
        this.emptyListener = emptyListener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    //  添加数据
    public void addData(int position, ExamData data) {
        list.add(position, data);
        //添加动画
        notifyItemInserted(position);
    }

    //  删除数据
    public void removeData(final int position) {
        UpdateOrDeleteExecutor executor = LitePal.deleteAsync(ExamData.class, list.get(position).getId());
        executor.listen(new UpdateOrDeleteCallback() {
            @Override
            public void onFinish(int rowsAffected) {
                if(rowsAffected>0){
                    list.remove(position);
                    //删除动画
                    notifyItemRemoved(position);
                    notifyDataSetChanged();
                    if(list.size()<1&&emptyListener!=null){
                        emptyListener.onEmpty();
                    }
                    Toast.makeText(context,"删除成功", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(context,"删除失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void setDataList(List<ExamData> examList) {
        list.clear();
        list.addAll(examList);
        notifyDataSetChanged();
    }

    /**
     * ViewHolder的类，用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_card_id, tv_weight, tv_create_time, tv_size,tv_data_remark;
        LinearLayout ll_data_container;

        MyViewHolder(View view) {
            super(view);
            tv_card_id = view.findViewById(R.id.tv_card_id);
            tv_weight = view.findViewById(R.id.tv_weight);
            tv_create_time = view.findViewById(R.id.tv_create_time);
            tv_size = view.findViewById(R.id.tv_size);
            tv_data_remark = view.findViewById(R.id.tv_data_remark);
            ll_data_container = view.findViewById(R.id.ll_data_container);
        }
    }
    EmptyListener emptyListener;
    public interface EmptyListener{
        void onEmpty();
    }
}
